package top.korori.common.model;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@ApiModel("分页数据")
@Setter
@Getter
public class PageData<T> {

    @ApiModelProperty("总记录数")
    private Long total;

    @ApiModelProperty("数据列表")
    private List<T> list;


    public static <O> PageData<O> setData(Page<O> page){
        PageData<O> pageData = new PageData<>();
        pageData.setList(page.getRecords());
        pageData.setTotal(page.getTotal());
        return pageData;
    }

    public static <O,Z> PageData<O> setDataCopyRes(Page<Z> page,Class<O> copyClass){
        PageData<O> pageData = new PageData<>();
        List<O> copyToList = BeanUtil.copyToList(page.getRecords(), copyClass);
        pageData.setList(copyToList);
        pageData.setTotal(page.getTotal());
        return pageData;
    }
}
